﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.IO;
public partial class Main_Reports_Inventory_StockReport : System.Web.UI.Page
{
    Datautility DU = new Datautility(); DataTable Dt = new DataTable();
    int TotalOpeningBalanceQty = 0; double TotalOpeningBalanceValue = 0;
    int TotalPurchaseQty = 0; double TotalPurchaseValue = 0;
    int TotalIssueQty = 0; double TotalIssuevalue = 0;
    int TotalReturnQty = 0; double TotalReturnValue = 0;
    int TotalClossingQty = 0; double TotalClossingValue = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //txtfromdate.Text = System.DateTime.Now.ToString(string.Format("dd/MM/yyyy"));           
            //GetData();
        }
    }
    protected void GetData()
    {
        try
        {
            string todate = "";
            string fromdate = "";
            DataTable dtstockDetail = DU.getdatausingprocedure("Usp_IVR_GetStockDetails");
            Dt.Columns.Add("ITMID", typeof(Int32));
            Dt.Columns.Add("ItemCode", typeof(string));
            Dt.Columns.Add("ItemName", typeof(string));
            Dt.Columns.Add("BinNo", typeof(string));
            Dt.Columns.Add("LocationID", typeof(string));
            Dt.Columns.Add("MPN", typeof(string));
            Dt.Columns.Add("ITEMGROUP", typeof(string));
            Dt.Columns.Add("ALTITEMCODE", typeof(string));
            Dt.Columns.Add("ItemAddlDesc", typeof(string));
            Dt.Columns.Add("Project", typeof(string));
            Dt.Columns.Add("LeasedType", typeof(string));
            Dt.Columns.Add("PurchaseQty", typeof(string));
            Dt.Columns.Add("PurchaseValue", typeof(string));
            Dt.Columns.Add("IssueQty", typeof(string));
            Dt.Columns.Add("IssueValue", typeof(string));
            Dt.Columns.Add("ReturnQty", typeof(string));
            Dt.Columns.Add("ReturnValue", typeof(string));
            Dt.Columns.Add("OpeningBalQty", typeof(int));
            Dt.Columns.Add("OpeningBalValue", typeof(string));
            Dt.Columns.Add("ClosingQty", typeof(string));
            Dt.Columns.Add("ClosingValue", typeof(string));
            Dt.Columns.Add("LandedCost", typeof(decimal));
            //Dt.Columns.Add("InventoryClass", typeof(string));
            double TempOpeningValue = 0; double TempPurValue = 0; string IssueItem = ""; string ReturnItem = "";
            double TempPurQty = 0; double TempIssueQty = 0; double TempPurIssueValue = 0;
            double tempRetrunQty=0; double TempRetrunValue=0;double TempBalanceQty = 0; 
            string MPNNo = ""; double singleqtyValue = 0; string Openingstock = ""; string Purchase = "";
            
            if (dtstockDetail.Rows.Count > 0)
            {
                if (txttodate.Text != "")
                {
                    todate = Convert.ToDateTime(txttodate.Text).ToString(string.Format("yyyy/MM/dd"));
                }
                else
                {
                    todate = System.DateTime.Now.ToString("yyyy/MM/dd");
                }
                if (txtfromdate.Text != "")
                {
                    fromdate = Convert.ToDateTime(txtfromdate.Text).ToString(string.Format("yyyy-MM-dd"));
                }
                else
                {
                    fromdate = "2014-04-01";
                }
                lblRecordNo.Text = "To Date" + todate;
                lblperiod.Text = "From Date" + fromdate;
                for (int i = 0; i < dtstockDetail.Rows.Count; i++)
                {
                    DataRow DR = Dt.NewRow();
                    //DR["ITMID"] = dtstockDetail.Rows[i]["ITMID"];                   
                    DR["ItemCode"] = dtstockDetail.Rows[i]["ItemCode"];
                    DR["ItemName"] = dtstockDetail.Rows[i]["ItemName"];
                    DR["BinNo"] = dtstockDetail.Rows[i]["BinNo"];
                    DR["ALTITEMCODE"] = dtstockDetail.Rows[i]["ALTITEMCODE"];
                    if (dtstockDetail.Rows[i]["LocationID"].ToString() == "")
                    {
                        DR["LocationID"] = "-";
                    }
                    else
                    {
                        DR["LocationID"] = dtstockDetail.Rows[i]["LocationID"];
                    }
                    string MpnSql = "select MPNCODE from IVR_ITEM_MPN where ITEMID=" + dtstockDetail.Rows[i]["ITMID"].ToString();
                    DataTable DTMPN = DU.getdata(MpnSql);
                    if (DTMPN.Rows.Count > 0)
                    {
                        for (int j = 0; j < DTMPN.Rows.Count; j++)
                        {
                            if (DTMPN.Rows[j]["MPNCODE"].ToString() != "")
                            {
                                MPNNo += DTMPN.Rows[j]["MPNCODE"].ToString() + ",";
                            }
                        }
                        if (MPNNo.Length > 0)
                        {
                            DR["MPN"] = MPNNo.Substring(0, MPNNo.Length - 1).Trim();
                        }
                    }
                    else
                    {
                        DR["MPN"] = "-";
                    }                    
                    DR["ITEMGROUP"] = dtstockDetail.Rows[i]["ITEMGROUP"];
                    if (dtstockDetail.Rows[i]["ItemAddlDesc"].ToString()=="0")
                    {
                        DR["ItemAddlDesc"] = "-";
                    }
                    else
                    {
                        DR["ItemAddlDesc"] = dtstockDetail.Rows[i]["ItemAddlDesc"];
                    }
                    DR["Project"] = dtstockDetail.Rows[i]["Project"];
                    DR["LeasedType"] = dtstockDetail.Rows[i]["LeasedType"];

                    Openingstock = "select ISNULL(CONVERT(decimal(18,0),Quantity),0) - ISNULL(CONVERT(decimal(18,0),IssueQty ),0) + ISNULL(CONVERT(decimal(18,0),RetrunQty ),0) as BalanceQty,ISNULL(RetrunQty,0) as RetrunQty,ISNULL(Quantity,0) as Quantity,ISNULL(IssueQty,0) as IssueQty,AMOUNT,RATE from IVR_ITEMOPENBAL where ITMID='" + dtstockDetail.Rows[i]["ITMID"] + "' and OPDATE = '" + fromdate + "'";
                    Purchase = "select ISNULL(CONVERT(decimal(18,0),IVR_TRAN_ITEM.ITMQTY),0)- ISNULL(CONVERT(decimal(18,0),IVR_TRAN_ITEM.IssueQty),0) as BalanceQTY, IVR_TRAN_ITEM.ITMQTY as ITMQTY,IVR_TRAN_ITEM.IssueQty as IssueQty,IVR_TRAN_ITEM.RATE as RATE,TOTALINVENTCOST, GROSSAMT from IVR_TRAN_ITEM inner join IVR_TRAN_MAIN on IVR_TRAN_MAIN.TRNID=IVR_TRAN_ITEM.TRNID where IVR_TRAN_ITEM.ITMID='" + dtstockDetail.Rows[i]["ITMID"] + "' and IVR_TRAN_ITEM.EntryType='PUR' and IVR_TRAN_MAIN.PurchaseRecDate Between '"+fromdate+"' and '" + todate + "'";
                    //IssueItem = "select ISNULL(IVR_OnlineRegistration_Detail.IssueQty,0) as IssueQty from IVR_OnlineRegistration_Detail inner join IVR_ITEM on IVR_OnlineRegistration_Detail.ItemCode=IVR_ITEM.ITMCODE inner join IVR_ITEMOPENBAL on IVR_ITEMOPENBAL.ITMID=IVR_ITEM.ITMID where IVR_ITEM.ITMID='" + dtstockDetail.Rows[i]["ITMID"] + "' and IVR_OnlineRegistration_Detail.IssuedDate between '" + fromdate + "' and '" + todate + "'";
                    IssueItem = "select SUM(ISSUEQTY)as ISSUEQTY,sum(ISSUEVALUE)as ISSUEVALUE from IVR_COMMONITEM_ISSUERETURN where ITMID='" + dtstockDetail.Rows[i]["ITMID"] + "' and ISSUEDATE between '" + fromdate + "' and '" + todate + "' and ENTRYTYPE <>'RET'";

                    //select SUM(ISSUEQTY)as ISSUEQTY,sum(ISSUEVALUE)as ISSUEVALUE from IVR_COMMONITEM_ISSUERETURN where ITMID='" + dtstockDetail.Rows[i]["ITMID"] + "' and ISSUEDATE between '" + fromdate + "' and '" + todate + "' and ENTRYTYPE = 'RET'";
                    ReturnItem = "select SUM(RETURNQTY)as RETURNQTY,sum(RETURNVALUE)as RETURNVALUE from IVR_COMMONITEM_ISSUERETURN where ITMID='" + dtstockDetail.Rows[i]["ITMID"] + "' and RETURNDATE between '" + fromdate + "' and '" + todate + "' and ENTRYTYPE = 'RET'";

                    DataTable DtOpening = DU.getdata(Openingstock);
                    DataTable DTPurchase = DU.getdata(Purchase);
                    DataTable DTIssueItem = DU.getdata(IssueItem);
                    DataTable DTReturnItem = DU.getdata(ReturnItem);
                    //Get The value from the Opening Balance Qty AN dValue from the IVR_ITEMOPENBAL
                    if (DtOpening.Rows.Count > 0)
                    {
                        TempBalanceQty = Convert.ToDouble(DtOpening.Rows[0]["Quantity"].ToString());
                        TempOpeningValue = Convert.ToDouble(DtOpening.Rows[0]["AMOUNT"].ToString()); //* Convert.ToDouble(DtOpening.Rows[0]["RATE"].ToString());
                    }
                    if (TempOpeningValue > 0)
                    {
                        singleqtyValue = (TempOpeningValue / TempBalanceQty);
                    }                    
                    //From the Purchase Receive
                    if (DTPurchase.Rows.Count > 0)
                    {
                        for (int j = 0; j < DTPurchase.Rows.Count; j++)
                        {
                            TempPurValue += Convert.ToDouble(DTPurchase.Rows[j]["GROSSAMT"].ToString());
                            TempPurQty += Convert.ToDouble(DTPurchase.Rows[j]["ITMQTY"].ToString());
                        }
                    }                    
                    //Issue Qty And Value.
                    if(DTIssueItem.Rows.Count>0)
                    {
                        if ( DTIssueItem.Rows[0]["ISSUEQTY"].ToString()!="")
                        {
                            TempIssueQty = Convert.ToDouble(DTIssueItem.Rows[0]["ISSUEQTY"].ToString());
                            TempPurIssueValue = Convert.ToDouble(DTIssueItem.Rows[0]["ISSUEVALUE"].ToString());
                        }
                    }
                    //Retrun Item ANd Value.
                    if (DTReturnItem.Rows.Count > 0)
                    {
                        if (DTReturnItem.Rows[0]["RETURNQTY"].ToString() != "")
                        {
                            tempRetrunQty = Convert.ToDouble(DTReturnItem.Rows[0]["RETURNQTY"].ToString());
                            TempRetrunValue = Convert.ToDouble(DTReturnItem.Rows[0]["RETURNVALUE"].ToString());
                        }
                    }
                    DR["OpeningBalQty"] = TempBalanceQty; //From the Opening Table of Item
                    DR["OpeningBalValue"] = TempOpeningValue.ToString("N2");// From the Opening value of the Item.

                    DR["PurchaseQty"] = TempPurQty; //From the Purchase Receive of the Item
                    DR["PurchaseValue"] = TempPurValue.ToString("N2");// From the Purchase Reveive value of the Item

                    DR["IssueQty"] = TempIssueQty; //Issue Qty come from the Issue table of the Item
                    DR["IssueValue"] = TempPurIssueValue.ToString("N2"); //After the calculation of the Opening Value as well as the Purchase Value.

                    DR["ReturnQty"] = tempRetrunQty;
                    DR["ReturnValue"] = TempRetrunValue.ToString("N2");

                    DR["ClosingQty"] = (TempBalanceQty + TempPurQty + tempRetrunQty) - TempIssueQty;// from the Opening Qty + Purchase Qty - Issue Qty + Item Return  OF the Item.
                    DR["ClosingValue"] = ((TempOpeningValue + TempPurValue + TempRetrunValue) - TempPurIssueValue).ToString("N2");//from the Opening Value + Purchase Value - Issue Value + Item Return VAlue  OF the Item.

                    /*Commented by the Krishna Sir.*/
                    //DR["LandedCost"] = TempLandedCost + TempOpeningValue;

                    //if ((TempBalanceQty + TempPurchaseQty + TempClosingQty + TempPurQtyclosing) > 500)
                    //{
                    //    DR["InventoryClass"] = "A";
                    //}
                    //else if ((TempBalanceQty + TempPurchaseQty + TempClosingQty + TempPurQtyclosing) >= 100 || (TempBalanceQty + TempPurchaseQty + TempClosingQty + TempPurQtyclosing) <= 499)
                    //{
                    //    DR["InventoryClass"] = "B";
                    //}
                    //else if ((TempBalanceQty + TempPurchaseQty + TempClosingQty + TempPurQtyclosing) <= 99)
                    //{                    
                    //    DR["InventoryClass"] = "C";
                    //}

                    Dt.Rows.Add(DR);
                    //Empty Data.
                    #region Empty the Variable
                    MPNNo = "";
                    TempOpeningValue = 0; TempPurValue = 0; TempIssueQty = 0; singleqtyValue = 0;
                    TempBalanceQty = 0; TempPurQty = 0;TempPurIssueValue = 0; tempRetrunQty = 0;TempRetrunValue = 0;
                    DtOpening.Clear();
                    DTPurchase.Clear();
                    #endregion
                }   
                grdStockReport.DataSource = Dt;
                //grdStockReport.AllowPaging = true;
                grdStockReport.DataBind();
                lblRecordNo.Text = grdStockReport.Rows.Count.ToString() + " Record(s)";
                lblperiod.Text = "Period :" + Convert.ToDateTime(fromdate).ToString(string.Format("dd-MM-yyyy")) + " to " + Convert.ToDateTime(todate).ToString(string.Format("dd-MM-yyyy"));
            }
            fromdate = ""; todate = "";
        }
        catch (Exception ex)
        {
        }
    }
    protected void grdStockReport_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdStockReport.PageIndex = e.NewPageIndex;
        GetData();
    }
    protected void btngetreport_Click(object sender, EventArgs e)
    {
        GetData();
    }
    protected void btnReport_Click(object sender, EventArgs e)
    {
        grdStockReport.Visible = true;
        GetData();
        string Filter = "";
        if (ddlColumnname.SelectedValue == "1")//for Item Code
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "ItemCode like'%" + txtValue.Text + "%'");
            //if (ChkConsigned.Checked == true)
            //{
            //    Filter = string.Format(CultureInfo.InvariantCulture, "ItemCode like'%" + txtValue.Text + "%' AND LeasedType='" + ChkConsigned.Text + "'");
            //}
            //if (ChkOwned.Checked == true)
            //{
            //    Filter = string.Format(CultureInfo.InvariantCulture, "ItemCode like'%" + txtValue.Text + "%' AND LeasedType='" + ChkOwned.Text + "'");
            //}
            //if (ChkConsigned.Checked == true && ChkOwned.Checked == true)
            //{
            //    Filter = string.Format(CultureInfo.InvariantCulture, "ItemCode like'%" + txtValue.Text + "%'");
            //}
            grdStockReport.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "2")//for Item name
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "ItemName Like'%" + txtValue.Text + "%'");
            //if (ChkConsigned.Checked == true)
            //{
            //    Filter = string.Format(CultureInfo.InvariantCulture, "ItemName like'%" + txtValue.Text + "%' AND LeasedType='" + ChkConsigned.Text + "'");
            //}
            //if (ChkOwned.Checked == true)
            //{
            //    Filter = string.Format(CultureInfo.InvariantCulture, "ItemName like'%" + txtValue.Text + "%' AND LeasedType='" + ChkOwned.Text + "'");
            //}
            //if (ChkConsigned.Checked == true && ChkOwned.Checked == true)
            //{
            //    Filter = string.Format(CultureInfo.InvariantCulture, "ItemName Like'%" + txtValue.Text + "%'");
            //}
            grdStockReport.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "3")//For Project
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "Project Like'%" + txtValue.Text + "%'");
            //if (ChkConsigned.Checked == true)
            //{
            //    Filter = string.Format(CultureInfo.InvariantCulture, "Project like'%" + txtValue.Text + "%' AND LeasedType='" + ChkConsigned.Text + "'");
            //}
            //if (ChkOwned.Checked == true)
            //{
            //    Filter = string.Format(CultureInfo.InvariantCulture, "Project like'%" + txtValue.Text + "%' AND LeasedType='" + ChkConsigned.Text + "'");
            //}
            //if (ChkConsigned.Checked == true && ChkOwned.Checked == true)
            //{
            //    Filter = string.Format(CultureInfo.InvariantCulture, "Project Like'%" + txtValue.Text + "%'");
            //}
            grdStockReport.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "4")//For BinNo
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "BinNo Like'%" + txtValue.Text + "%'");
            //if (ChkConsigned.Checked == true)
            //{
            //    Filter = string.Format(CultureInfo.InvariantCulture, "BinNo like'%" + txtValue.Text + "%' AND LeasedType='" + ChkConsigned.Text + "'");
            //}
            //if (ChkOwned.Checked == true)
            //{
            //    Filter = string.Format(CultureInfo.InvariantCulture, "BinNo like'%" + txtValue.Text + "%' AND LeasedType='" + ChkConsigned.Text + "'");
            //}
            //if (ChkConsigned.Checked == true && ChkOwned.Checked == true)
            //{
            //    Filter = string.Format(CultureInfo.InvariantCulture, "BinNo Like'%" + txtValue.Text + "%'");
            //}            
            grdStockReport.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "5")//For MPN
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "MPN Like'%" + txtValue.Text + "%'");
            //if (ChkConsigned.Checked == true)
            //{
            //    Filter = string.Format(CultureInfo.InvariantCulture, "MPN like'%" + txtValue.Text + "%' AND LeasedType='" + ChkConsigned.Text + "'");
            //}
            //if (ChkOwned.Checked == true)
            //{
            //    Filter = string.Format(CultureInfo.InvariantCulture, "MPN like'%" + txtValue.Text + "%' AND LeasedType='" + ChkConsigned.Text + "'");
            //}
            //if (ChkConsigned.Checked == true && ChkOwned.Checked == true)
            //{
            //    Filter = string.Format(CultureInfo.InvariantCulture, "MPN Like'%" + txtValue.Text + "%'");
            //}
            grdStockReport.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "6")//Item Group
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "ItemGroup='" + txtValue.Text + "'");
            //if (ChkConsigned.Checked == true)
            //{
            //    Filter = string.Format(CultureInfo.InvariantCulture, "ItemGroup='" + txtValue.Text + "' AND LeasedType='" + ChkConsigned.Text + "'");
            //}
            //if (ChkOwned.Checked == true)
            //{
            //    Filter = string.Format(CultureInfo.InvariantCulture, "ItemGroup='" + txtValue.Text + "' AND LeasedType='" + ChkConsigned.Text + "'");
            //}
            //if (ChkConsigned.Checked == true && ChkOwned.Checked == true)
            //{
            //    Filter = string.Format(CultureInfo.InvariantCulture, "ItemGroup='" + txtValue.Text + "'");
            //}
            grdStockReport.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "7") //For the Inventory Class
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "InventoryClass='" + txtValue.Text + "'");
            grdStockReport.AllowPaging = false;
        }
        //else if (ChkConsigned.Checked == true)// for Consigned
        //{
        //    if (ChkOwned.Checked != false)
        //    {                
        //        GetData();
        //    }
        //    else
        //    {
        //        Filter = string.Format(CultureInfo.InvariantCulture, "LeasedType='" + ChkConsigned.Text + "'");
        //        grdStockReport.AllowPaging = false;
        //    }
        //}
        //else if (ChkOwned.Checked == true) //For owned
        //{
        //    if (ChkConsigned.Checked != false)
        //    {
        //        GetData();
        //    }
        //    else
        //    {
        //        Filter = string.Format(CultureInfo.InvariantCulture, "LeasedType='" + ChkOwned.Text + "'");
        //        grdStockReport.AllowPaging = false;
        //    }
        //}
        //else if(ChkConsigned.Checked==false && ChkOwned.Checked==false) // for Consigned and Owned Are not Checked.
        //{
        //    grdStockReport.Visible = false;
        //}
        else
        {
            GetData();
        }

        DataRow[] Dr = Dt.Select(Filter);
        DataTable Tempdt = new DataTable();
        Tempdt = Dt.Clone();
        foreach (DataRow drNew in Dr)
        {
            Tempdt.ImportRow(drNew);
        }
        grdStockReport.DataSource = Tempdt;
        grdStockReport.DataBind();
    }
    protected void grdStockReport_RowDataBound(object sender, GridViewRowEventArgs e)
    {        
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //Label LblOpeningBalanaceQty=(Label) e.Row.FindControl("lblopeningQty");
            // OpeningBalanceQty +=Convert.ToInt32(LblOpeningBalanaceQty.Text);
            TotalOpeningBalanceQty += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "OpeningBalQty"));
            TotalOpeningBalanceValue += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "OpeningBalValue"));
            TotalPurchaseQty += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "PurchaseQty"));
            TotalPurchaseValue += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "PurchaseValue"));
            TotalIssueQty += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "IssueQty"));
            TotalIssuevalue += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "IssueValue"));
            TotalReturnQty += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ReturnQty"));
            TotalReturnValue += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "ReturnValue"));
            TotalClossingQty += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ClosingQty"));
            TotalClossingValue += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "ClosingValue"));
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            Label lblTotalQty = (Label)e.Row.FindControl("lblOpeningtotal");
            lblTotalQty.Text = TotalOpeningBalanceQty.ToString();
            Label lblTotalValue = (Label)e.Row.FindControl("lblOpeningTotalValue");
            lblTotalValue.Text = TotalOpeningBalanceValue.ToString();

            Label LblTotalPurchaseQty = (Label)e.Row.FindControl("lblPurchaseTotalQty");
            LblTotalPurchaseQty.Text = TotalPurchaseQty.ToString();
            Label LblTotalPurchaseValue = (Label)e.Row.FindControl("lblPurchaseTotalValue");
            LblTotalPurchaseValue.Text = TotalPurchaseValue.ToString();

            Label LblTotalIssueQty = (Label)e.Row.FindControl("lblTotalIssueQty");
            LblTotalIssueQty.Text = TotalIssueQty.ToString();
            Label LblTotalIssueValue = (Label)e.Row.FindControl("lblTotalIssueValue");
            LblTotalIssueValue.Text = TotalIssuevalue.ToString();

            Label LblTotalReturnQty = (Label)e.Row.FindControl("lblTotalReturnQty");
            LblTotalReturnQty.Text = TotalReturnQty.ToString();
            Label LblTotalReturnValue = (Label)e.Row.FindControl("lblTotalReturnValue");
            LblTotalReturnValue.Text = TotalReturnValue.ToString();

            Label LblTotalClossingQty = (Label)e.Row.FindControl("lbltotalClossingQty");
            LblTotalClossingQty.Text = TotalClossingQty.ToString();
            Label LblTotalClossingValue = (Label)e.Row.FindControl("lbltotalClossingValue");
            LblTotalClossingValue.Text = TotalClossingValue.ToString();
        }
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }
    protected void ImgBtnExcel_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Stock_Report.xls"));
            Response.ContentType = "application/ms-excel";
            grdStockReport.ShowFooter = false;
            grdStockReport.AllowPaging = false;
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            grdStockReport.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();
        }
        catch (Exception ex)
        {
        }
    }
}